Importing tab delimited file into array in Visual Basic 2013 [migrated]
Posted
by
JaceG
on Programmers
See other posts from Programmers
or by JaceG
Published on 2014-06-10T05:17:57Z
Indexed on
2014/06/10
9:40 UTC
Read the original article
Hit count: 284
file-handling
|visual-basic
I am needing to import a tab delimited text file that has 11 columns and an unknown number of rows (always minimum 3 rows).
I would like to import this text file as an array and be able to call data from it as needed, throughout my project. And then, to make things more difficult, I need to replace items in the array, and even add more rows to it as the project goes on (all at runtime).
Hopefully someone can suggest code corrections or useful methods. I'm hoping to use something like the array style sMyStrings(3,2), which I believe would be the easiest way to control my data.
Any help is gladly appreciated, and worthy of a slab of beer. Here's the coding I have so far:
Imports System.IO
Imports Microsoft.VisualBasic.FileIO
Public Class Main
Dim strReadLine As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sReader As IO.StreamReader = Nothing
Dim sRawString As String = Nothing
Dim sMyStrings() As String = Nothing
Dim intCount As Integer = -1
Dim intFullLoop As Integer = 0
If IO.File.Exists("C:\MyProject\Hardware.txt") Then ' Make sure the file exists
sReader = New IO.StreamReader("C:\MyProject\Hardware.txt")
Else
MsgBox("File doesn't exist.", MsgBoxStyle.Critical, "Error")
End
End If
Do While sReader.Peek >= 0 ' Make sure you can read beyond the current position
sRawString = sReader.ReadLine() ' Read the current line
sMyStrings = sRawString.Split(New Char() {Chr(9)}) ' Separate values and store in a string array
For Each s As String In sMyStrings ' Loop through the string array
intCount = intCount + 1 ' Increment
If TextBox1.Text <> "" Then TextBox1.Text = TextBox1.Text & vbCrLf ' Add line feed
TextBox1.Text = TextBox1.Text & s ' Add line to debug textbox
If intFullLoop > 14 And intCount > -1 And CBool((intCount - 0) / 11 Mod 0) Then
cmbSelectHinge.Items.Add(sMyStrings(intCount))
End If
Next
intCount = -1
intFullLoop = intFullLoop + 1
Loop
End Sub
© Programmers or respective owner